#!/bin/bash

MAXINDEX="--max-index -1"
TRANSPORT="--transport zeromq"
VERBOSE="--severity INFO"

while [[ $# > 1 ]]
do
key="$1"

case $key in
    -m|--max-index)
	MAXINDEX="--max-index $2"
	shift
	;;
    -t|--transport)
	TRANSPORT="--transport $2"
	shift
	;;
    -v|--verbose)
	VERBOSE="--severity $2"
	shift
	;;
esac
shift
done


########################### Define some variables
# ASCII and ROOT parameter files for the processor device
ROOTPARAM="@pixel_file_dir@/macros/pixel_@pixel_simulation_engine@.params.root"
ASCIIPARAM="@pixel_file_dir@/param/pixel_digi.par"

FAIRTASKNAME1="--task-name PixelFindHits"
FAIRTASKNAME2="--task-name PixelFindTracks --keep-data PixelHits"
FAIRTASKNAME3="--task-name PixelFitTracks"
# input file and branch for the sampler device
INPUTFILE="@pixel_file_dir@/macros/pixel_@pixel_simulation_engine@.digi.root"
INPUTBRANCH="PixelDigis"

# output file for sink
OUTPUTFILE="@pixel_file_dir@/macros/MQ.pixel_@pixel_simulation_engine@.3Levels.fitTracks.root"
###########################


########################### Start the chain of the devices


########################## start Parameter server
SERVER="parmq-server $TRANSPORT"
SERVER+=" --id parmq-server --channel-config name=param,type=rep,method=bind,rateLogging=0,address=tcp://*:5305  --channel-name param"
SERVER+=" --first-input-name $ROOTPARAM --second-input-name $ASCIIPARAM --second-input-type ASCII"
xterm -geometry 80x22+0+350 -hold -e @fairroot_bin_dir@/$SERVER &


########################## start SAMPLER
SAMPLER="pixel-sampler $TRANSPORT --severity info"
SAMPLER+=" --id sampler1 --channel-config name=data-out,type=push,method=bind,rateLogging=1,address=tcp://*:5306"
SAMPLER+=" --file-name $INPUTFILE --branch-name $INPUTBRANCH --branch-name EventHeader. $MAXINDEX"
xterm -geometry 80x22+0+0 -hold -e @pixel_bin_dir@/$SAMPLER &

########################## start PROCESSORs on level 1
PROCESSOR1_1="pixel-processor $TRANSPORT"
PROCESSOR1_1+=" $VERBOSE $FAIRTASKNAME1 --id processor1_1"
PROCESSOR1_1+=" --channel-config name=param,type=req,method=connect,rateLogging=0,address=tcp://localhost:5305"
PROCESSOR1_1+=" --channel-config name=data-in,type=pull,method=connect,rateLogging=1,address=tcp://localhost:5306"
PROCESSOR1_1+=" --channel-config name=data-out,type=push,method=connect,rateLogging=1,address=tcp://localhost:5311"
xterm -geometry 80x22+500+0 -hold -e @pixel_bin_dir@/$PROCESSOR1_1 &

PROCESSOR1_2="pixel-processor $TRANSPORT"
PROCESSOR1_2+=" $VERBOSE $FAIRTASKNAME1 --id processor1_2"
PROCESSOR1_2+=" --channel-config name=param,type=req,method=connect,rateLogging=0,address=tcp://localhost:5305"
PROCESSOR1_2+=" --channel-config name=data-in,type=pull,method=connect,rateLogging=1,address=tcp://localhost:5306"
PROCESSOR1_2+=" --channel-config name=data-out,type=push,method=connect,rateLogging=1,address=tcp://localhost:5321"
xterm -geometry 80x22+1000+0 -hold -e @pixel_bin_dir@/$PROCESSOR1_2 &

########################## start PROCESSORs on level 2
PROCESSOR2_1="pixel-processor $TRANSPORT"
PROCESSOR2_1+=" $VERBOSE $FAIRTASKNAME2 --id processor2_1"
PROCESSOR2_1+=" --channel-config name=param,type=req,method=connect,rateLogging=0,address=tcp://localhost:5305"
PROCESSOR2_1+=" --channel-config name=data-in,type=pull,method=bind,rateLogging=1,address=tcp://*:5311"
PROCESSOR2_1+=" --channel-config name=data-out,type=push,method=bind,rateLogging=1,address=tcp://*:5312"
xterm -geometry 80x22+500+350 -hold -e @pixel_bin_dir@/$PROCESSOR2_1 &

PROCESSOR2_2="pixel-processor $TRANSPORT"
PROCESSOR2_2+=" $VERBOSE $FAIRTASKNAME2 --id processor2_2"
PROCESSOR2_2+=" --channel-config name=param,type=req,method=connect,rateLogging=0,address=tcp://localhost:5305"
PROCESSOR2_2+=" --channel-config name=data-in,type=pull,method=bind,rateLogging=1,address=tcp://*:5321"
PROCESSOR2_2+=" --channel-config name=data-out,type=push,method=bind,rateLogging=1,address=tcp://*:5322"
xterm -geometry 80x22+1000+350 -hold -e @pixel_bin_dir@/$PROCESSOR2_2 &

########################## start PROCESSORs on level 3
PROCESSOR3_1="pixel-processor $TRANSPORT"
PROCESSOR3_1+=" $VERBOSE $FAIRTASKNAME3 --id processor3_1"
PROCESSOR3_1+=" --channel-config name=param,type=req,method=connect,rateLogging=0,address=tcp://localhost:5305"
PROCESSOR3_1+=" --channel-config name=data-in,type=pull,method=connect,rateLogging=1,address=tcp://localhost:5312"
PROCESSOR3_1+=" --channel-config name=data-out,type=push,method=connect,rateLogging=1,address=tcp://localhost:5330"
xterm -geometry 80x22+500+700 -hold -e @pixel_bin_dir@/$PROCESSOR3_1 &

PROCESSOR3_2="pixel-processor $TRANSPORT"
PROCESSOR3_2+=" $VERBOSE $FAIRTASKNAME3 --id processor3_2"
PROCESSOR3_2+=" --channel-config name=param,type=req,method=connect,rateLogging=0,address=tcp://localhost:5305"
PROCESSOR3_2+=" --channel-config name=data-in,type=pull,method=connect,rateLogging=1,address=tcp://localhost:5322"
PROCESSOR3_2+=" --channel-config name=data-out,type=push,method=connect,rateLogging=1,address=tcp://localhost:5330"
xterm -geometry 80x22+1000+700 -hold -e @pixel_bin_dir@/$PROCESSOR3_2 &

########################## start FILESINK
FILESINK="pixel-sink $TRANSPORT"
FILESINK+=" --id sink1 --channel-config name=data-in,type=pull,method=bind,rateLogging=1,address=tcp://*:5330"
FILESINK+=" --file-name $OUTPUTFILE"
xterm +aw -geometry 80x22+0+700 -hold -e @pixel_bin_dir@/$FILESINK &


